// fs_wmall/paotui/pages/buy/buy.js
var a = getApp();
var base = require('../../../utils/lang.js');
const _ = base._
import authUser from '../../../utils/auth'

import {
	pickerTap,
	bindMultiPickerColumnChange,
	loadData,
	loadHoursMinute,
	loadMinute,
	bindStartMultiPickerChange
} from '../../date.js'

Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		content: '',
		startAddr: _('去哪里办'),
		detailAddr: '',
		tel: '',
		startDate: _("请选择日期"),
		multiArray: [
			[],
			[],
			[]
		],
		multiIndex: [0, 0, 0],
		time: ['1', '2', '3', '4', '5', '6', '7', '8', ],
		time_index: 0,
		tip: [],
		tip_index: -1,
		isAgreement: true,
		money: '0.00',

	},
	pickerTap,
	bindMultiPickerColumnChange,
	loadData,
	loadHoursMinute,
	loadMinute,
	bindStartMultiPickerChange,
	submitFn() {
		const {
			isAgreement
		} = this.data
		if (!isAgreement) {
			wx.showToast({
				title: _('请勾选同意协议按钮'),
				icon: 'none'
			})
		} else {
			this.buyFn()
		}
	},
	agreementFn() {
		let {
			isAgreement
		} = this.data
		this.setData({
			isAgreement: !isAgreement
		})
	},
	telFn(e) {
		const {
			value
		} = e.detail
		this.setData({
			tel: value
		})
	},
	detailAddrFn(e) {
		const {
			value
		} = e.detail
		this.setData({
			detailAddr: value
		})
	},
	startFn() {
		wx.chooseLocation({
			success: res => {
				const {
					address
				} = res
				this.setData({
					startAddr: address
				})
			}
		})
	},
	endFn() {
		wx.chooseLocation({
			success: res => {
				const {
					address
				} = res
				this.setData({
					endAddr: address
				})
			}
		})
	},
	contentFn(e) {
		const {
			value
		} = e.detail
		this.setData({
			content: value
		})
	},
	bindTimeChange(e) {
		this.setData({
			time_index: e.detail.value
		})
	},
	bindTipChange(e) {
		this.setData({
			tip_index: e.detail.value
		})
	},
	init() {
		a.util.request({
			'url': 'entry/wxapp/ErranderTake',
			success: res => {
				let {
					take_tiplow,
					take_tipheight,
					take_premoney
				} = res.data
				take_tiplow = parseInt(take_tiplow)
				take_tipheight = parseInt(take_tipheight)
				let tipArr = []
				for (var i = take_tiplow; i <= take_tipheight; i++) {
					let item = i + _('元')
					tipArr.push(item)
				}
				this.setData({
					tip: tipArr,
					money: take_premoney
				})
			},
		})
	},
	buyFn() {
		const _openId = wx.getStorageSync('_openId')
		let {
			content,
			startAddr,
			detailAddr,
			money,
			tel,
			startDate,
			time,
			time_index,
			tip,
			tip_index
		} = this.data
		let _tip = 0
		if (tip_index >= 0) {
			_tip = tip[tip_index]
		}
		if (startAddr === _('去哪里办')) {
			wx.showToast({
				title: _('请选择去哪里办'),
				icon: 'none'
			})
			return
		}
		if (tel === '') {
			wx.showToast({
				title: _('请填写电话'),
				icon: 'none'
			})
			return
		}
		if (startDate === _('请选择日期')) {
			wx.showToast({
				title: _('请选择日期'),
				icon: 'none'
			})
			return
		}
		let data = {
			type: 3,
			uid: _openId,
			content: content,
			start: startAddr,
			detailAddr: detailAddr,
			money: money,
			tel: tel,
			date: startDate,
			time: time[time_index],
			tip: _tip
		}
		a.util.request({
			'url': 'entry/wxapp/ErranderAddorder',
			data: data,
			success: res => {
				console.log('zcj')
				console.log(res)
				if (res.data.errcode === 0) {
					this.payFn(res.data)
				}
			},
		})
	},
	payFn(res) {
		wx.requestPayment({
			'timeStamp': res.data.timeStamp,
			'nonceStr': res.data.nonceStr,
			'package': res.data.package,
			'signType': res.data.signType,
			'paySign': res.data.paySign,
			'success': function(res) {
				console.log(res)
				wx.showToast({
					title: _('支付成功'),
				})
				setTimeout(function() {
					wx.redirectTo({
						url: '/fs_wmall/pages/wddd/order'
					})
				}, 1500)
			},
			'fail': function(res) {
				console.log(res)
				wx.showLoading({
					title: _('支付失败'),
				})
			}
		})
	},
	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
		let userInfo = wx.getStorageSync('userInfo') || "{}"
		userInfo = JSON.parse(userInfo)
		const redirect = '/fs_wmall/paotui/pages/serve/serve'
		if (!authUser(userInfo, redirect)) {
			return
		}
		this.init()
		const isWei = base.isWeiFn()
		this.setData({
			isWei,
		})
		this.pickerTap()
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function() {
		this.setData({
			_t: base._t()
		})
	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function() {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function() {

	}
})
